<?php
/**
  +------------------------------------------------------------------------------
 * 系统设置
  +------------------------------------------------------------------------------
 */
class SysConfAction extends CommonAction {

    /**
     *系统设置界面 
     */
    public function index() {
        $list = D('SysConf')->where("status=1 and group_id > 0 and is_show = 1")->order("sort asc")->select();
        $conf_list = array(); //用于输出分组格式化后的数组
        
        foreach ($list as $k => $v) {
            $v['val_arr'] = explode(",", $v['val_arr']);
            $conf_list[L("SYSCONF_GROUP_" . $v['group_id'])][$k] = $v;
        }
        $this->assign("conf_list", $conf_list);
        $this->display();
    }

    public function update() {
        $upload_list = $this->uploadImages(0,'logo');
        if ($upload_list) {
            foreach ($upload_list as $upload_item) {
                if ($upload_item['key'] == "0") {
                    $site_logo = $upload_item['recpath'] . $upload_item['savename'];
                    $site_logo='temp/'.$site_logo;
//                    $site_logo = moveFile($site_logo, './logo.gif');
                }
                if ($upload_item['key'] == "3") {
                    $water_image = $upload_item['recpath'] . $upload_item['savename'];
                    $water_image='temp/'.$water_image;
                }
                if ($upload_item['key'] == "1") {
                    $foot_logo = $upload_item['recpath'] . $upload_item['savename'];
                    $foot_logo='temp/'.$foot_logo;
//                    $foot_logo = moveFile($foot_logo, './foot_logo.gif');
                }
                if ($upload_item['key'] == "2") {
                    $link_logo = $upload_item['recpath'] . $upload_item['savename'];
                    $link_logo='temp/'.$link_logo;
//                    $link_logo = moveFile($link_logo, './link_logo.gif');
                }
            }
        }

        $list = D('SysConf')->where("status=1")->select();
        foreach ($list as $k => $v) {
            $v['val'] = isset($_REQUEST[$v['name']]) ? $_REQUEST[$v['name']] : $v['val'];
            if ($v['name'] == "SITE_LOGO" && !empty($site_logo)) {
                if ($site_logo != $v['val']) {
//                    @unlink(__ROOT__ . '/temp/logo/'. $v['val']);
                    $v['val'] = $site_logo;
                }
            }

            if ($v['name'] == "WATER_IMAGE" && !empty($water_image)) {
                if ($water_image != $v['val']) {
//                    @unlink(__ROOT__ . '/temp/logo/'.$v['val']);
                    $v['val'] = $water_image;
                }
            }

            if ($v['name'] == "FOOT_LOGO" && !empty($foot_logo)) {
                if ($foot_logo != $v['val']) {
//                    @unlink(__ROOT__ . '/temp/logo/'. $v['val']);
                    $v['val'] = $foot_logo;
                }
            }

            if ($v['name'] == "LINK_LOGO" && !empty($link_logo)) {
                if ($link_logo != $v['val']) {
//                    @unlink(__ROOT__ . '/temp/logo/'. $v['val']);
                    $v['val'] = $link_logo;
                }
            }

            D('SysConf')->save($v);
        }

        $this->saveLog(1);
        $this->success(L('EDIT_SUCCESS'));
    }

}

//function moveFile($file_name, $target_name) {
//    $name = $file_name;
//
//    if (function_exists("move_uploaded_file")) {
//        if (move_uploaded_file(FANWE_ROOT . $file_name, FANWE_ROOT . $target_name)) {
//            chmod(FANWE_ROOT . $target_name, 0755);
//            $name = $target_name;
//            unlink(FANWE_ROOT . $file_name);
//        } else if (copy(FANWE_ROOT . $file_name, FANWE_ROOT . $target_name)) {
//            chmod(FANWE_ROOT . $target_name, 0755);
//            $name = $target_name;
//            unlink(FANWE_ROOT . $file_name);
//        }
//    } elseif (copy(FANWE_ROOT . $file_name, FANWE_ROOT . $target_name)) {
//        chmod(FANWE_ROOT . $target_name, 0755);
//        $name = $target_name;
//        unlink(FANWE_ROOT . $file_name);
//    }
//
//    return $name;
//}

?>